<template>
  <div class="box">
    <div class="flex">
      <div>
        <el-icon>
          <Document />
        </el-icon><span>数据列表</span>
      </div>
      <div>
        <el-button type="primary">添加</el-button>
      </div>
    </div>
  </div>

  <!-- <template> -->
  <el-table :data="tableData" style="width: 100%" border="true">
    <el-table-column align="center" prop="id" label="编号"> </el-table-column>
    <el-table-column align="center" prop="title" label="菜单名称"></el-table-column>
    <el-table-column align="center" prop="level" label="菜单级数">一级</el-table-column>
    <el-table-column align="center" prop="name" label="前端名称"></el-table-column>
    <el-table-column align="center" prop="icon" label="前端图标"></el-table-column>
    <el-table-column align="center" label="是否显示">
      <template #default="scope">
        <el-switch v-model="scope.row.hidden" :active-value="1" :inactive-value="0" />
      </template>
    </el-table-column>
    <el-table-column align="center" prop="sort" label="排序"></el-table-column>
    <el-table-column align="center" prop="Setup" label="设置">
      <template #default="scope">
        <el-button link type="primary" @click="handleEdit(scope.$index, scope.row)">查看下级</el-button>
      </template>
    </el-table-column>

    <el-table-column align="center" prop="operate" label="操作">
      <template #default="scope">
        <el-button link type="primary" @click="handleEdit(scope.$index, scope.row)">
          编辑
        </el-button>
      </template>
    </el-table-column>

  </el-table>
  <!-- </template> -->
  <!-- 分页设置 -->
  <ElConfigProvider :locale="zhCn">
    <el-pagination v-model:currentPage="listMenu.pageNum" v-model:page-size="listMenu.pageSize"
      @size-change="pageSizeChange" @current-change="pageNumChange" background
      layout="total,sizes,prev, pager, next,jumper" :total="total" />
  </ElConfigProvider>
</template>
<!-- @current-change="handleCurrentChange" -->
<script lang="ts" setup>
import { ElConfigProvider } from 'element-plus'
import { Document, SetUp } from '@element-plus/icons-vue'
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { reactive, ref, toRefs } from 'vue'
import { useRouter } from 'vue-router'
import { user } from "@/api"
import { ElIcon, ElButton, ElTable, ElTableColumn } from 'element-plus';


const router = useRouter();
//这一步是点击跳转的关键
console.log(router.getRoutes())

const listMenu = reactive({
  parentId: 0,
  pageNum: 1,
  pageSize: 10,
})

//请求数据 封装
const Menulist = () => {
  user.getMenuListApi(listMenu).then((res) => {
    if (res.code === 200) {
      console.log(res.data);
      state.tableData = res.data.list
      state.total = res.data.total
    }
  })
}
Menulist();

// 返回的数据的类型约束
interface IState {
  tableData: IMenuModel[];
  total: number
}

const state = reactive<IState>({
  tableData: [],
  total: 0
})

const pageSizeChange = (val: number) => {
  console.log(val);
  Menulist();
}

const pageNumChange = (val: number) => {
  Menulist();
}

const handleEdit = (index: number, row: IMenuModel) => {
  console.log(index, row)
}
const handleDelete = (index: number, row: IMenuModel) => {
  console.log(index, row)
}

let { tableData, total } = toRefs(state);

</script>

<style lang="less" scoped>
* {
  overflow: hidden;
}

.box {
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 10px;

  .flex {
    display: flex;
    justify-content: space-between;
  }
}

.el-icon {
  vertical-align: middle;
  margin-right: 2px;
}

.el-table {
  margin-top: 50px;
}

.el-pagination {
  margin-left: 800px;
}
</style>